package com.hzy.quetosys.controller;

import com.github.pagehelper.PageInfo;
import com.hzy.quetosys.entity.Customer;
import com.hzy.quetosys.entity.Order;
import com.hzy.quetosys.service.CustomerService;
import com.hzy.quetosys.service.OrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Controller
public class OrderController {
    private static final Logger logger= LoggerFactory.getLogger(OrderController.class);
    @Autowired
    private OrderService orderService;
    @GetMapping("order/order_add")
    public ModelAndView orderAddView(){
        ModelAndView mav=new ModelAndView("order/order_add");
        return mav;
    }
    @GetMapping("order/order_list")
    public ModelAndView orderListView(
            @RequestParam(name = "pageNum",required = true,defaultValue = "1") int pageNum,
            @RequestParam(name = "pageSize",required = true,defaultValue = "5") int pageSize,
            @RequestParam(name = "keyword",required = false) String keyword
    ){
        ModelAndView mav=new ModelAndView("order/order_list");

        PageInfo<Order> page=orderService.findByKeyword(pageNum,pageSize,keyword);
        mav.addObject("page",page);
//        mav.addObject(page);
        return mav;
    }
    @GetMapping("/order/order_modify")
    public ModelAndView orderModifyView(
            @RequestParam(name = "pageNum",required = true,defaultValue = "1") int pageNum,
            @RequestParam(name = "pageSize",required = true,defaultValue = "5") int pageSize,
            @RequestParam(name = "id" ,required = true) int id,
            @RequestParam(name = "keyword",required = false) String keyword
    ){
        ModelAndView mav=new ModelAndView("order/order_modify");
        Order order=orderService.findById(id);
        mav.addObject("order",order);
        return mav;
    }
    @PostMapping("/order/order_add")
    public ModelAndView orderAdd(@Validated Order order, BindingResult bindingResult){

        ModelAndView mav=new ModelAndView("redirect:/order/order_add");
        Map<String,String> errorMessages=new HashMap<>();
        if(bindingResult.hasErrors()){
            final List<FieldError> fieldErrors = bindingResult.getFieldErrors();
            for(FieldError error:fieldErrors){
                errorMessages.put(error.getField(),error.getDefaultMessage());
            }
            mav.addObject("errorMessages",errorMessages);
            mav.addObject("order",order);
            mav.setViewName("order/order_add");
        }else {
            boolean result= orderService.append(order);
            if(result){
                logger.info("新增客户信息成功-"+order);
            }else {
                logger.info("新增客户信息失败-"+order);
            }
        }

        return mav;
    }
    @PostMapping("/order/order_modify")
    public ModelAndView orderModify(
            @RequestParam(name = "pageNum",required = true,defaultValue = "1") int pageNum,
            @RequestParam(name = "pageSize",required = true,defaultValue = "5") int pageSize,
            @RequestParam(name = "keyword",required = false) String keyword,
            Order order
    ) throws UnsupportedEncodingException {
        String redirectUrl="redirect:/order/order_list?pageNum="+pageNum+"&pageSize="+pageSize+"&keyword="+ URLEncoder.encode(keyword,"utf-8");
        ModelAndView mav=new ModelAndView(redirectUrl);
        boolean result=orderService.modify(order);
        return mav;
    }
    @GetMapping("/order/order_remove")
    public ModelAndView orderRemove(
            @RequestParam(name = "pageNum",required = true,defaultValue = "1") int pageNum,
            @RequestParam(name = "pageSize",required = true,defaultValue = "5") int pageSize,
            @RequestParam(name = "id" ,required = true) int id,
            @RequestParam(name = "keyword",required = false) String keyword
    ) throws UnsupportedEncodingException {
        String redirectUrl="redirect:/order/order_list?pageNum="+pageNum+"&pageSize="+pageSize+"&keyword="+ URLEncoder.encode(keyword,"utf-8");
        ModelAndView mav=new ModelAndView(redirectUrl);
        boolean result=orderService.remove(id);
        return mav;
    }
}
